/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components.filebrowser;

import br.com.pc9.pswing.util.SystemUtilsOS;
import br.com.pc9.pswing.util.UserFolders;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author paulocanedo
 */
public class FileBrowserModelFactory {

    private FileBrowserListModel browserListModel;
    private ListModel favoritePlacesListModel;
    private FileNavigationComboBoxModel navigationComboBoxModel;

    protected FileBrowserListModel createBrowserListModel() {
        return new FileBrowserListModel();
    }

    protected ListModel createHotFilesListModel() {
        return new HotFilesListModel();
    }

    protected FileNavigationComboBoxModel createNavigationComboBoxModel() {
        return new FileNavigationComboBoxModel();
    }

    public FileBrowserListModel getBrowserListModel() {
        if (browserListModel == null) {
            browserListModel = createBrowserListModel();
        }
        return browserListModel;
    }

    public ListModel getFavoritePlacesListModel() {
        if (favoritePlacesListModel == null) {
            favoritePlacesListModel = createHotFilesListModel();
        }
        return favoritePlacesListModel;
    }

    public FileNavigationComboBoxModel getNavigationComboBoxModel() {
        if (navigationComboBoxModel == null) {
            navigationComboBoxModel = createNavigationComboBoxModel();
        }
        return navigationComboBoxModel;
    }

    private class HotFilesListModel extends DefaultListModel {

        private List<PFileView> hotfiles = new ArrayList<PFileView>();

        private List<PFileView> getHotfiles() {
            if (hotfiles.isEmpty()) {
                try {
                    UserFolders userFolders = SystemUtilsOS.getUserFolders();
                    File homeFile = SystemUtilsOS.getUserHomeAsFile();
                    File[] files = new File[]{homeFile, userFolders.getDocumentFolder(), userFolders.getMusicFolder(),
                        userFolders.getPicturesFolder(), userFolders.getVideosFolder(), userFolders.getDownloadsFolder(), userFolders.getDesktopFolder()};
                    for (File file : files) {
                        if (file != null && file.exists()) {
                            hotfiles.add(new PFileView(file));
                        }
                    }

//                    FileSystemView fileSystemView = FileSystemView.getFileSystemView();
//                    for (File file : fileSystemView.getRoots()) {
//                        hotfiles.add(new PFileView(file));
//                    }
                } catch (Exception ex) {
                    Logger.getLogger(FileBrowserModelFactory.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return hotfiles;
        }

        @Override
        public int getSize() {
            return getHotfiles().size();
        }

        @Override
        public Object getElementAt(int index) {
            return getHotfiles().get(index);
        }
    }
}
